package com.ctf.soft.ms.service.manage.impl;

import com.ctf.soft.ms.dao.dto.manage.PurviewMenu;
import com.ctf.soft.ms.dao.mapper.manage.MenuMapper;
import com.ctf.soft.ms.dao.mapper.manage.MenuRelationMapper;
import com.ctf.soft.ms.dao.model.manage.Menu;
import com.ctf.soft.ms.dao.model.manage.MenuRelation;
import com.ctf.soft.ms.service.impl.BaseService;
import com.ctf.soft.ms.service.manage.IMenuService;
import com.ctf.soft.ms.utils.ResourceUtil;
import com.ctf.soft.ms.utils.ToolsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 名称： MenuServiceImpl<br>
 * 描述： 系统菜单业务处理实现类<br>
 * 最近修改时间：2015/7/31 13:16<br>
 * 修改说明：
 *
 * @author jiajl
 */
@Service
public class MenuServiceImpl extends BaseService<Menu> implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private MenuRelationMapper menuRelationMapper;

    public List<Menu> loadPageList(Map<String, Object> map) {
        int page = Integer.valueOf(map.get("page").toString());
        int rows = Integer.valueOf(map.get("rows").toString());
        int beginRowNum = (page - 1) * rows;
        int endRowNum = page * rows;
        map.put("beginRowNum", beginRowNum);
        map.put("endRowNum", endRowNum);
        map.put("db", ResourceUtil.getString("DATA_ALIAS"));
        return menuMapper.queryForPageList(map);
    }

    public int getPageCount(Map<String, Object> map) {
        return menuMapper.queryForPageCount(map);
    }

    /**
     * 读取权限菜单
     */
    public List<PurviewMenu> loadPurviewMenu(String mark) {
        List<PurviewMenu> list = new ArrayList<PurviewMenu>();
        Menu params = new Menu();
        params.setParentId("0");
        params.setStatus("Y");
        if(!ToolsUtil.isEmpty(mark)){
            params.setMenuMark(mark);
        }
        List<Menu> menuList = selectByList(params);
        for (Menu menu : menuList) {
            PurviewMenu purviewMenu = new PurviewMenu();
            purviewMenu.setId(menu.getId().trim());
            purviewMenu.setMenuName(menu.getMenuName().trim());
            purviewMenu.setMenuKey(menu.getMenuKey().trim());
            params.setParentId(menu.getId().trim());
            purviewMenu.setChildMenuList(selectByList(params));
            list.add(purviewMenu);
        }
        return list;
    }

    public int insertBatch(String parentMenu, String menuCode, String menuName) {
        List<MenuRelation> list = new ArrayList<MenuRelation>();
        String[] arrsCode = menuCode.split(",");
        String[] arrsName = menuName.split(",");
        for (int i = 0; i < arrsCode.length; i++) {
            MenuRelation menuRelation = new MenuRelation();
            int len = arrsCode[i].lastIndexOf("]") + 1;

            list.add(menuRelation);
        }
        return menuRelationMapper.insertBatch(list);
    }

    public List<MenuRelation> loadListByRelation(String mark, String code) {
        MenuRelation params = new MenuRelation();
        params.setRelationMark(mark);
        params.setRelationCode(code);
        return menuRelationMapper.select(params);
    }
}
